﻿Imports System.Data.OleDb
Imports System.Xml
'adapted from tutorial found at: http://www.dotnettutorials.com/tutorials/advanced/rss-feed-asp-net-vb.aspx

Public Class StoryFeed
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Clear()
        Response.ContentType = "application/rss+xml"
        Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        objX.WriteStartDocument()
        objX.WriteStartElement("rss")
        objX.WriteAttributeString("version", "2.0")
        objX.WriteStartElement("channel")

        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("AbuDhabi_Speech_ConnectionString").ConnectionString)
        Dim SqlCommand As String = "SELECT TOP 10 * From Posts Where Type=1 ORDER BY ID DESC "
        Dim cmd As OleDbCommand = New OleDbCommand(SqlCommand, oleDbConn)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        objX.WriteElementString("title", "Most Recent Stories from the ADSD Network")
        objX.WriteElementString("link", "Link to be Defined later")
        objX.WriteElementString("description", "The Abu Dhabi Speech Delay Network offers support to expatriate parents of children who are experiencing speech delays. It offers a spce to share stories and resources and arrange to meetup for face to face support.")
        objX.WriteElementString("language", "en-us")
        objX.WriteElementString("ttl", "60")
        'objX.WriteElementString("image", "http://vbasic.net/media/logo.gif")
        objX.WriteElementString("lastBuildDate", String.Format("{0:R}", DateTime.Now))

        Do While dr.Read()
            objX.WriteStartElement("item")
            objX.WriteElementString("title", dr("Title").ToString())
            objX.WriteElementString("author", dr("Author").ToString())
            objX.WriteElementString("link", "Story_Details&StoryID=" & dr("ID").ToString())
            objX.WriteStartElement("guid")
            objX.WriteAttributeString("isPermaLink", "true")
            objX.WriteString("Story_Details&StoryID=" & dr("ID").ToString())
            objX.WriteEndElement()
            objX.WriteElementString("pubDate", String.Format("{0:R}", dr("Date_Posted")))
            objX.WriteStartElement("category")
            objX.WriteString("story")
            objX.WriteEndElement()
            Dim postDetails As String = dr("Post_details").ToString()
            If postDetails.Length() > 100 Then
                postDetails = postDetails.Substring(0, 100) & ".."
            End If
            objX.WriteElementString("description", postDetails)
            objX.WriteEndElement()
        Loop

        objX.WriteEndElement()
        objX.WriteEndElement()
        objX.WriteEndDocument()
        objX.Flush()
        objX.Close()
        Response.End()
    End Sub

End Class